package com.itheima.controller.backend;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.Setmeal;
import com.itheima.service.MedicalExaminationPackageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class MedicalExaminationPackageController {


    @Autowired
    private MedicalExaminationPackageService medicalExaminationPackageService;

    @Autowired
    private AliOssUtil aliOssUtil;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    //新增体检套餐
    @PostMapping("/add")
    public Result addMedicalExaminationPackage(@RequestParam("checkgroupIds") List<Integer> checkgroupIds, @RequestBody Setmeal setmeal) {
        log.info("新增体检套餐Setmeal:{}", setmeal);
        log.info("新增体检套餐checkgroupIds:{}", checkgroupIds);
        medicalExaminationPackageService.addMedicalExaminationPackage(setmeal, checkgroupIds);
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    //上传图片
    @PostMapping("/upload")
    public Result upload( MultipartFile imgFile) throws Exception {
        log.info("上传图片:{}", imgFile);

        String url = aliOssUtil.upload(imgFile.getBytes(), imgFile.getOriginalFilename());

        redisTemplate.opsForSet().add("setmealPicDbUrl", url);
        return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, url);
    }

    //分页查询体检套餐
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return medicalExaminationPackageService.findPage(queryPageBean);
    }

    //根据id查询体检套餐
    @GetMapping("/findById")
    public Result findById(@RequestParam("id") Integer id) {
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, medicalExaminationPackageService.findById(id));
    }

    //编辑体检套餐
    @PostMapping("/edit")
    public Result editMedicalExaminationPackage(@RequestParam("checkGroupIds") List<Integer> checkGroupIds,@RequestBody Setmeal setmeal){


        medicalExaminationPackageService.editMedicalExaminationPackage(checkGroupIds,setmeal);
        return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
    }

}
